<template>
  <div class="order-detail">
    <el-card class="box-card" style="margin-bottom: 20px;">
      <div slot="header" class="clearfix">
        <span style="font-weight: bold;">订单基本信息</span>
      </div>
      <el-row :gutter="20">
        <el-col :span="12">
          <div class="detail-item">
            <span class="label">订单号：</span>
            <span class="value">{{ orderInfo.orderNumber }}</span>
          </div>
          <div class="detail-item">
            <span class="label">订单状态：</span>
            <el-tag :type="getStatusType(orderInfo.status)">{{ getStatusText(orderInfo.status) }}</el-tag>
          </div>
          <div class="detail-item">
            <span class="label">支付方式：</span>
            <span class="value">{{ getPaymentMethodText(orderInfo.paymentMethod) }}</span>
          </div>
          <div class="detail-item">
            <span class="label">订单备注：</span>
            <span class="value">{{ orderInfo.remark || '无' }}</span>
          </div>
        </el-col>
        <el-col :span="12">
          <div class="detail-item">
            <span class="label">下单时间：</span>
            <span class="value">{{ parseTime(orderInfo.createTime) }}</span>
          </div>
          <div class="detail-item">
            <span class="label">支付时间：</span>
            <span class="value">{{ parseTime(orderInfo.payTime) || '未支付' }}</span>
          </div>
          <div class="detail-item">
            <span class="label">完成时间：</span>
            <span class="value">{{ parseTime(orderInfo.completeTime) || '未完成' }}</span>
          </div>
          <div class="detail-item">
            <span class="label">取消原因：</span>
            <span class="value">{{ orderInfo.cancelReason || '无' }}</span>
          </div>
        </el-col>
      </el-row>
    </el-card>

    <el-card class="box-card" style="margin-bottom: 20px;">
      <div slot="header" class="clearfix">
        <span style="font-weight: bold;">用户信息</span>
      </div>
      <el-row :gutter="20">
        <el-col :span="12">
          <div class="detail-item">
            <span class="label">用户姓名：</span>
            <span class="value">{{ orderInfo.userName || '未知用户' }}</span>
          </div>
          <div class="detail-item">
            <span class="label">联系电话：</span>
            <span class="value">{{ orderInfo.userPhone || '无' }}</span>
          </div>
        </el-col>
        <el-col :span="12">
          <div class="detail-item">
            <span class="label">配送地址：</span>
            <span class="value">{{ orderInfo.deliveryAddress || '无' }}</span>
          </div>
        </el-col>
      </el-row>
    </el-card>

    <el-card class="box-card" style="margin-bottom: 20px;">
      <div slot="header" class="clearfix">
        <span style="font-weight: bold;">商家信息</span>
      </div>
      <el-row :gutter="20">
        <el-col :span="12">
          <div class="detail-item">
            <span class="label">店铺名称：</span>
            <span class="value">{{ orderInfo.shopName || '未知商家' }}</span>
          </div>
        </el-col>
        <el-col :span="12">
          <div class="detail-item">
            <span class="label">商家地址：</span>
            <span class="value">{{ orderInfo.shopAddressDetail || '无' }}</span>
          </div>
        </el-col>
      </el-row>
    </el-card>

    <el-card class="box-card" style="margin-bottom: 20px;">
      <div slot="header" class="clearfix">
        <span style="font-weight: bold;">金额信息</span>
      </div>
      <el-row :gutter="20">
        <el-col :span="12">
          <div class="detail-item">
            <span class="label">商品总价：</span>
            <span class="value">¥{{ orderInfo.productAmount }}</span>
          </div>
          <div class="detail-item">
            <span class="label">配送费：</span>
            <span class="value">¥{{ orderInfo.deliveryFee }}</span>
          </div>
          <div class="detail-item">
            <span class="label">优惠金额：</span>
            <span class="value">¥{{ orderInfo.discountAmount }}</span>
          </div>
        </el-col>
        <el-col :span="12">
          <div class="detail-item">
            <span class="label">订单总价：</span>
            <span class="value" style="color: #ff4d4f; font-weight: bold; font-size: 18px;">¥{{ orderInfo.totalAmount }}</span>
          </div>
          <div class="detail-item">
            <span class="label">平台抽成：</span>
            <span class="value">¥{{ orderInfo.commissionAmount }}</span>
          </div>
        </el-col>
      </el-row>
    </el-card>

    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span style="font-weight: bold;">商品详情</span>
      </div>
      <el-table :data="productDetails" border style="width: 100%">
        <el-table-column prop="productName" label="商品名称" width="200">
          <template slot-scope="scope">
            <div style="display: flex; align-items: center;">
              <img 
                v-if="scope.row.productImage" 
                :src="scope.row.productImage" 
                style="width: 50px; height: 50px; margin-right: 10px; border-radius: 4px;"
                @error="handleImageError"
              />
              <span>{{ scope.row.productName }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="description" label="商品描述" width="200">
          <template slot-scope="scope">
            <span>{{ scope.row.description || '无描述' }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="quantity" label="数量" width="80" align="center" />
        <el-table-column prop="originalPrice" label="原价" width="100" align="center">
          <template slot-scope="scope">
            <span>¥{{ scope.row.originalPrice }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="actualPrice" label="成交价" width="100" align="center">
          <template slot-scope="scope">
            <span>¥{{ scope.row.actualPrice }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="refundStatus" label="退款状态" width="100" align="center">
          <template slot-scope="scope">
            <el-tag :type="getRefundStatusType(scope.row.refundStatus)">
              {{ getRefundStatusText(scope.row.refundStatus) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="refundAmount" label="退款金额" width="100" align="center">
          <template slot-scope="scope">
            <span>¥{{ scope.row.refundAmount }}</span>
          </template>
        </el-table-column>
        <el-table-column label="小计" width="100" align="center">
          <template slot-scope="scope">
            <span style="font-weight: bold;">¥{{ (scope.row.actualPrice * scope.row.quantity).toFixed(2) }}</span>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import { getOrderInfo, getOrderProductDetails } from "@/api/order/order";

export default {
  name: "OrderDetail",
  props: {
    orderId: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      orderInfo: {},
      productDetails: [],
      loading: true
    };
  },
  watch: {
    orderId: {
      immediate: true,
      handler(newVal) {
        if (newVal) {
          this.getOrderDetail();
        }
      }
    }
  },
  methods: {
    /** 获取订单详情 */
    getOrderDetail() {
      this.loading = true;
      
      // 获取订单基本信息（包含用户、商家等扩展信息）
      getOrderInfo(this.orderId).then(response => {
        this.orderInfo = response.data;
        this.getProductDetails();
        console.log("orderinfo",response);
      }).catch(error => {
        console.error('获取订单信息失败:', error);
        this.$modal.msgError("获取订单信息失败");
        this.loading = false;
      });
    },
    
    /** 获取商品详情 */
    getProductDetails() {
      getOrderProductDetails(this.orderId).then(response => {
        console.log(this.orderId);
        this.productDetails = response.data || [];
        this.loading = false;
        console.log("orderdetail",response);
      }).catch(error => {
        console.error('获取商品详情失败:', error);
        this.$modal.msgError("获取商品详情失败");
        this.loading = false;
      });
    },
    /** 获取订单状态文本 */
    getStatusText(status) {
      const statusMap = {
        0: '待支付',
        1: '已支付待接单',
        2: '已接单制作中',
        3: '配送中',
        4: '已完成',
        5: '已取消',
        6: '已退款'
      };
      return statusMap[status] || '未知状态';
    },
    /** 获取订单状态类型 */
    getStatusType(status) {
      const typeMap = {
        0: 'warning',
        1: 'primary',
        2: 'info',
        3: 'warning',
        4: 'success',
        5: 'danger',
        6: 'info'
      };
      return typeMap[status] || '';
    },
    /** 获取支付方式文本 */
    getPaymentMethodText(method) {
      const methodMap = {
        1: '支付宝',
        2: '余额支付'
      };
      return methodMap[method] || '未知支付方式';
    },
    /** 获取退款状态文本 */
    getRefundStatusText(status) {
      const statusMap = {
        0: '未退款',
        1: '部分退款',
        2: '全部退款'
      };
      return statusMap[status] || '未知状态';
    },
    /** 获取退款状态类型 */
    getRefundStatusType(status) {
      const typeMap = {
        0: 'success',
        1: 'warning',
        2: 'danger'
      };
      return typeMap[status] || '';
    },
    /** 图片加载错误处理 */
    handleImageError(e) {
      e.target.src = 'http://127.0.0.1:9300/statics/123.jpg';
    }
  }
};
</script>

<style scoped>
.order-detail {
  padding: 0;
}

.detail-item {
  margin-bottom: 15px;
  display: flex;
  align-items: center;
}

.detail-item .label {
  width: 100px;
  color: #666;
  font-weight: 500;
}

.detail-item .value {
  flex: 1;
  color: #333;
}

.box-card {
  border-radius: 8px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both;
}
</style>
